import Foundation
import WidgetKit
import SwiftUI

public class Preference: NSObject {
    public static let shared = Preference()
    
    #if os(macOS)
    @AppStorage(AssociationKey.startAtLogin, store: .group)
    public var startUp = false  // 自启动

    @AppStorage(AssociationKey.showDockIcon, store: .group)
    public var showDockIcon = false // Dock 栏显示图标

    @AppStorage(AssociationKey.menuBar, store: .group)
    public var menuBar = MenubarStyle.icon(.logo)   // 状态栏样式

    @AppStorage(AssociationKey.compactMode, store: .group)
    public var compactMode = true   // 紧凑模式

    @AppStorage(AssociationKey.timeReminds, store: .group)
    public var timeReminds = false  // 整点报时

    @AppStorage(AssociationKey.movePopup, store: .group)
    public var movePopup = true    // 移入弹出
    #endif
    
    @AppStorage(AssociationKey.theme, store: .group)
    public var theme = Theme.system {  // 主题
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }
    
    @AppStorage(AssociationKey.showReminder, store: UserDefaults.group)
    public var showReminder: Bool = false { // 显示提醒事项
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }
    
    @AppStorage(AssociationKey.showEvent, store: UserDefaults.group)
    public var showEvent: Bool = false { // 显示日历事件
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }
    
    @AppStorage(AssociationKey.showLunar, store: UserDefaults.group)
    public var showLunar: Bool = true {    // 显示农历节日
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }
    
    @AppStorage(AssociationKey.showFestival, store: UserDefaults.group)
    public var showFestival: Bool = true { // 显示节假日
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }
    
    @AppStorage(AssociationKey.language, store: UserDefaults.group)
    public var language: Language = .system {  // 语言
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }
    
    @AppStorage(AssociationKey.festivalPush, store: UserDefaults.group)
    public var festivalPush: Bool = false  // 节假日推送
    
    @AppStorage(AssociationKey.pushTime, store: UserDefaults.group)
    public var pushTime: PushTime = PushTime()  // 节假日时间
    
    @AppStorage(AssociationKey.pushSound, store: UserDefaults.group)
    public var pushSound: Bool = true  // 节假日推送是否开启铃声
    
    @AppStorage(AssociationKey.pushBadge, store: UserDefaults.group)
    public var pushBadge: Bool = false  // 节假日推送是否开启角标
    
    @AppStorage(AssociationKey.onlyShowCurrentMonth, store: UserDefaults.group)
    public var onlyShowCurrentMonth: Bool = false {  // 只显示当前月
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }
    
    @AppStorage(AssociationKey.firstWeek, store: UserDefaults.group)
    public var firstWeek: Week = .sun  {    // 星期开始于周日
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }
    
    @AppStorage(AssociationKey.sync, store: UserDefaults.group)
    public var sync: Bool = true {    // 同步配置
        didSet {
            WidgetCenter.shared.reloadAllTimelines()
        }
    }
    
    // MARK: 颜色
    @AppStorage(AssociationKey.accent, store: .group)
    public var accent: ColorModel = ColorModel.accent {    // 主色调
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }
    
    @AppStorage(AssociationKey.background, store: .group)
    public var background: ColorModel = ColorModel.background {    // 背景
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }
    
    @AppStorage(AssociationKey.normal, store: .group)
    public var normal: ColorModel = ColorModel.normal {    // 一般日期
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }
    
    @AppStorage(AssociationKey.week, store: .group)
    public var week: ColorModel = ColorModel.week {    // 周末
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }
    
    @AppStorage(AssociationKey.festival, store: .group)
    public var festival: ColorModel = ColorModel.festival {    // 节日
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }
    
    @AppStorage(AssociationKey.current, store: .group)
    public var current: ColorModel = ColorModel.current {    // 当前
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }
    
    @AppStorage(AssociationKey.calendar, store: .group)
    public var calendar: ColorModel = ColorModel.calendar {    // 日历事件
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }
    
    @AppStorage(AssociationKey.reminder, store: .group)
    public var reminder: ColorModel = ColorModel.reminder {    // 提醒事件
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }
    
    @AppStorage(AssociationKey.work, store: .group)
    public var work: ColorModel = ColorModel.work {    // 加班
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }
    
    @AppStorage(AssociationKey.rest, store: .group)
    public var rest: ColorModel = ColorModel.rest {    // 休息
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }

    @AppStorage(AssociationKey.taboo, store: .group)
    public var taboo: ColorModel = ColorModel.taboo {    //  宜
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }

    @AppStorage(AssociationKey.avoid, store: .group)
    public var avoid: ColorModel = ColorModel.avoid {    // 忌
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }

    @AppStorage(AssociationKey.normalFont, store: .group)
    public var normalFont: FontModel = FontModel(name: "PingFangSC-Regular", size: 16) {
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }

    @AppStorage(AssociationKey.tagFont, store: .group)
    public var tagFont: FontModel = FontModel(name: "PingFangSC-Regular", size: 14) {
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }

    @AppStorage(AssociationKey.titleFont, store: .group)
    public var titleFont: FontModel = FontModel(name: "PingFangSC-Semibold", size: 20) {
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }

    @AppStorage(AssociationKey.subtitleFont, store: .group)
    public var subtitleFont: FontModel = FontModel(name: "PingFangSC-Regular", size: 12) {
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }

    @AppStorage(AssociationKey.weekFont, store: .group)
    public var weekFont: FontModel = FontModel(name: "PingFangSC-Medium", size: 16) {
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }

    @AppStorage(AssociationKey.dateFont, store: .group)
    public var dateFont: FontModel = FontModel(name: "PingFangSC-Medium", size: 16) {
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }

    @AppStorage(AssociationKey.festivalFont, store: .group)
    public var festivalFont: FontModel = FontModel(name: "PingFangSC-Regular", size: 12) {
        didSet {
            sync ? WidgetCenter.shared.reloadAllTimelines() : nil
        }
    }

    @AppStorage(AssociationKey.isResetable, store: .group)
    public var isResetable: Bool = false   // 是否可以重置颜色配置
    
    // 当前语言
    public static var language: Language {
        if Preference.shared.language == .system {
            guard let languages = UserDefaults.standard.object(forKey: "AppleLanguages") as? [String], let current = languages.first else { return .cn }
            if current.contains("zh-Hans") {
                return .cn
            } else if current.contains("zh-Hant") {
                return .tr
            } else {
                return .en
            }
        } else {
            return Preference.shared.language
        }
    }
    
}
